﻿using DJ.ZEF.ThirdParty;
using NSJL.Biz.Background.Client;
using NSJL.DAL.DataModel.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace DJ.Clients
{
    /// <summary>
    /// FailTipView.xaml 的交互逻辑
    /// </summary>
    public partial class FailTipView : Window
    {
        public int boxNum {  get; set; }
        public string type {  get; set; }
        DispatcherTimer timer = new DispatcherTimer();
        public FailTipView(int BoxNum,string Type)
        {
            InitializeComponent();
            lb1.Content = BoxNum + "号柜门未关闭，请关闭箱门!";
            VoiceHelper.GetInstance().Start(lb1.Content.ToString());
            boxNum = BoxNum;
            type = Type;
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += ((a, b) =>
            {
                var miao = Label1.Tag.ToInt32() - 1;
                Label1.Tag = miao;

                if (miao <= 55)
                {
                    //检测箱门
                    LockManager.GetInstance().CheckBox(BoxNum, (isclose) =>
                    {
                        if (isclose == false)
                        {
                            Dispatcher.Invoke(() =>
                            {
                                Close();
                            });
                        }
                    });
                }
                if (miao >= 0 && miao <= 30 && (miao % 3 == 0))
                {
                    //开启警报声音
                    VoiceHelper.GetInstance().Start("请关闭箱门");
                }
                if (miao <= 0)
                {
                    this.Close();
                    return;
                }
                Label1.Content = Label1.Tag + "s";
            });
            timer.IsEnabled = true;
            timer.Start();
        }

        CommBiz biz = new CommBiz();
        private void Window_Closed(object sender, EventArgs e)
        {
            timer.Stop();
            biz.AddFailInfo($"{boxNum}号箱门{type}未关闭");
        }
    }
}
